var total_DR = 0;
var total_CR = 0;
var i=1;
function add_row(){
        if(check_value()){                
                var dl = document.createElement('dl');
                dl.setAttribute('class','fin-form2'); 
                dl.setAttribute('className','fin-form2'); 
                dl.setAttribute('id','item'+i); 
                
                var dd = document.createElement('dd');
                var account_id = document.getElementById('account_id0').value;
                dd.innerHTML='<input type="hidden" name="account_id" id="account_id'+i+'" value="'+account_id+'"/><a href="/bin/finance.py/show_account_details?a='+account_id+'">'+account_id+'</a>';
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');
                var dr_amount = document.getElementById('dr_amount0').value;
                var dr_value = 0;
                if(dr_amount==''){
                        dd.innerHTML='<input type="hidden" name="dr_amount" id="dr_amount'+i+'" value="" />&nbsp;';
                }else{
                        dr_value += parseFloat(dr_amount);
                        dd.innerHTML='<input type="hidden" name="dr_amount" id="dr_amount'+i+'" value="'+dr_amount+'"/>'+dr_amount;
                }                
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');
                var cr_amount = document.getElementById('cr_amount0').value;
                var cr_value = 0;
                if(cr_amount==''){
                        dd.innerHTML='<input type="hidden" name="cr_amount" id="cr_amount'+i+'" value="" />&nbsp;';
                }else{
                        cr_value += parseFloat(cr_amount);
                        dd.innerHTML='<input type="hidden" name="cr_amount" id="cr_amount'+i+'" value="'+cr_amount+'"/>'+cr_amount;
                }                
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');                
                var description = document.getElementById('description0').value;
                dd.innerHTML='<div style="display:none"><textarea name="description" id="description'+i+'">'+description+'</textarea></div>'+escapeHTML(description);
                dl.appendChild(dd); 
                
                var dt = document.createElement('dt');
                dt.innerHTML = '<a href="javascript:void(0)" onclick="del_row(\'item'+i+'\');del_up('+dr_value+','+cr_value+')">Delete</a>';
                dl.appendChild(dt);
                
                var obj = document.getElementById("journal_list");
                obj.appendChild(dl);
                sum_up(dr_value,cr_value);
            
                i += 1;                
                clean_up();
        }
}
function check_value(){
        var account_id = document.getElementById('account_id0').value;
        var dr_amount = document.getElementById('dr_amount0').value;
        var cr_amount = document.getElementById('cr_amount0').value;
        var description = document.getElementById('description0').value;        
        var bool = true;
        
        if(account_id==""||(dr_amount==""&&cr_amount=="")||description=="")
        {               
                bool = false;
        }
        if(dr_amount!=""&&cr_amount!="")
        {
                document.getElementById('cr_amount0').value = "";
                document.getElementById('dr_amount0').value = "";
                bool = false;
        }  
        return bool;
}

function check_balance(){
        //var total_DR = 0;
        //var total_CR = 0;
        
        //for(j=1;j<i;j++){
        //        var dr_amount = document.getElementById("dr_amount"+j).value;
          //      if(dr_amount!=''){
            //            total_DR += parseFloat(dr_amount);
              //  }
                //var cr_amount = document.getElementById("cr_amount"+j).value;
                //if(cr_amount!=''){
                //        total_CR += parseFloat(cr_amount);
                //}
        //}
        if(total_DR==total_CR&&total_DR!=0){
                return true;
        }
        else
        {
                return false;
        }
}
function clean_up(){   
        document.getElementById('account_id0').value='';
        document.getElementById('dr_amount0').value='';
        document.getElementById('cr_amount0').value='';
        document.getElementById('description0').value='';
        disabled_obj();
        
}
function submit_form(id,func){
        form=document.getElementById(id);
        if(func){
                form.submit();
        }else{
                alert('Not balance!');
                return;
        }        
}
function disabled_obj(){
		var obj1 = document.getElementById('dr_amount0');
		var obj2 = document.getElementById('cr_amount0');
		if(obj1.value.trim().length==0 && obj2.value.trim().length==0){				
				//obj1.disabled=null;
				//obj2.disabled=null;
				obj1.style.display='block';
				obj2.style.display='block';
				return;
		}
		if(obj1.value.trim().length>0){
				obj2.style.display='none';
				//obj2.disabled='disabled';
				return;
		}
		if(obj2.value.trim().length>0){
				//obj1.disabled='disabled';
				obj1.style.display='none';
				return;
		}   
}
function del_row(id){    
    del_Tr = document.getElementById(id);
    len = del_Tr.childNodes.length;
    for(j=0;j<len;j++){
            del_Tr.childNodes[j].innerHTML='';
    }
    del_Tr.style.display='none';
}
function sum_up(dr,cr){
    if(dr=='0'){
            total_DR += 0;
    }else{
            total_DR += parseFloat(dr);
    }
    if(cr=='0'){
            total_CR += 0;
    }else{
            total_CR += parseFloat(cr);
    }
}
function del_up(dr,cr){
    if(dr=='0'){
            total_DR -= 0;
    }else{
            total_DR -= parseFloat(dr);
    }
    if(cr=='0'){
            total_CR -= 0;
    }else{
            total_CR -= parseFloat(cr);
    }
}
